package com.spr.servlet.model;

import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class ItemObject {
	/*
	 * Your AWS Access Key ID, as taken from the AWS Your Account page.
	 */
	private static final String AWS_ACCESS_KEY_ID = "AKIAJO7AWPBUJ2O4KAAQ";

	/*
	 * Your AWS Secret Key corresponding to the above ID, as taken from the AWS
	 * Your Account page.
	 */

	private static final String AWS_SECRET_KEY = "81uvQguYTCFKkWnefoasdhluTEsDV+rQctzG/aZw";
	
	/*
	 * Use one of the following end-points, according to the region you are
	 * interested in:
	 * 
	 *      US: ecs.amazonaws.com 
	 *      CA: ecs.amazonaws.ca 
	 *      UK: ecs.amazonaws.co.uk 
	 *      DE: ecs.amazonaws.de 
	 *      FR: ecs.amazonaws.fr 
	 *      JP: ecs.amazonaws.jp
	 * 
	 */
	private static final String ENDPOINT = "webservices.amazon.com";
	
	private String ItemID;
	private SignedRequestsHelper helper;
	private Map<String, String> params = new HashMap<String, String>();
	
	public ItemObject(){
		try {
			helper = SignedRequestsHelper.getInstance(ENDPOINT, AWS_ACCESS_KEY_ID, AWS_SECRET_KEY);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void setItemID(String input){
		ItemID=input;
		if(input.contains("/product/"))
			ItemID = input.substring(input.indexOf("/product/")+9, input.indexOf("/product/")+19);
		else if(input.contains("/dp/"))
			ItemID = input.substring(input.indexOf("/dp/")+4, input.indexOf("/dp/")+14);
		else if(input.contains("/ASIN/"))
			ItemID = input.substring(input.indexOf("/ASIN/")+6, input.indexOf("/ASIN/")+16);
		else if(input.contains("detail/-/"))
			ItemID = input.substring(input.indexOf("/detail/-/")+10, input.indexOf("/detail/-/")+20);
	}
	
	public String getItemID(){
		return ItemID;
	}
	
	public void resetMAP(){
		params.clear();
		params.put("Service", "AWSECommerceService");
		params.put("Version", "2009-03-31");
		params.put("AssociateTag", "aztag-20");
		params.put("ItemId", ItemID);
	}
	
	public String getTitle() {
		resetMAP();
		params.put("Operation", "ItemLookup");
		
		String requestUrl = helper.sign(params);
		String title = null;
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(requestUrl);
			Node titleNode = doc.getElementsByTagName("Title").item(0);
			if(titleNode!=null)
				title = titleNode.getTextContent();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return title;
	}
	
	public String getDesc() {
		resetMAP();
		params.put("Operation", "ItemLookup");
		params.put("ResponseGroup", "Large");
		
		String requestUrl = helper.sign(params);
		String title = null;
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(requestUrl);
			Node titleNode = doc.getElementsByTagName("Content").item(0);
			if(titleNode!=null)
				title = titleNode.getTextContent();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return title;
	}
	
	public String getPrice() {
		resetMAP();
		params.put("Operation", "ItemLookup");
		params.put("ResponseGroup", "Offers");
		
		String requestUrl = helper.sign(params);
		String price = null;
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(requestUrl);
			Node priceNode = doc.getElementsByTagName("FormattedPrice").item(0);
			if(priceNode!=null){
				price = priceNode.getTextContent();
				System.out.println("requestURL = "+requestUrl);
				}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		
		return price;
	}
	
	public String getVariesPrice() {
		resetMAP();
		params.put("Operation", "ItemLookup");
		params.put("ResponseGroup", "VariationOffers");
		
		String requestUrl = helper.sign(params);
		String price = null;
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(requestUrl);
			Node priceNode = doc.getElementsByTagName("VariationSummary").item(0);
			if(priceNode!=null){
				Element element = (Element)priceNode;
				Node priceList = element.getElementsByTagName("LowestPrice").item(0);
				element = (Element)priceList;
				String lowPrice = element.getElementsByTagName("FormattedPrice").item(0).getTextContent();
				
				element = (Element)priceNode;
				priceList = element.getElementsByTagName("HighestPrice").item(0);
				element = (Element)priceList;
				String highPrice = element.getElementsByTagName("FormattedPrice").item(0).getTextContent();
				
				price = lowPrice +" - "+highPrice;
				System.out.println("requestURL = "+requestUrl);
			} else 
				return getPrice();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		
		return price;
	}
	
}



